import uuid
from datetime import datetime

from flask import Blueprint, render_template, request, jsonify, session
from flask_login import login_required
from flask_restful import marshal, fields
from sqlalchemy import and_
from werkzeug.security import generate_password_hash

from applications.common import curd
from applications.common.curd import model_to_dicts, enable_status, disable_status, get_one_by_id, enable_is_on_sale, \
    disable_is_on_sale, auto_model_jsonify
from applications.common.helper import ModelFilter
from applications.common.utils.getopenid import get_openid, token
from applications.common.utils.http import table_api, success_api, fail_api, wxuser_success_api, orders_api, \
    orders_out_api, remark_api
from applications.common.utils.rights import authorize
from applications.common.utils.validate import xss_escape
from applications.exts import db
from applications.exts.DBA import DBA_text
from applications.exts.cache import cache
from applications.models import Role, Power, User, Goods, Classify, Orders, Wx_User_Address, Goods_Remark
from applications.models.wx_user import WxUser
from applications.schemas import RoleOutSchema, PowerOutSchema2, WxUserOutSchema, GoodsOutSchema, OrdersOutSchema
from applications.common.utils import upload as upload_curd

remark = Blueprint('remark', __name__, url_prefix='/remark')


# =====wx===============
# 添加评论
@remark.route('/addremark', methods=["POST"])
def addremark():
    req_json = request.json
    openid = req_json.get('openid')
    gid = req_json.get('gid')
    content = req_json.get('content')
    start = req_json.get('start')

    print(req_json)
    gremark = Goods_Remark()
    gremark.openid = openid
    # 过滤信息里面的敏感内容
    gremark.content = DBA_text(content)
    gremark.gid = gid
    gremark.start = start

    db.session.add(gremark)
    db.session.commit()

    dbremark = Goods_Remark.query.all()
    remarklist = []
    for item in dbremark:
        remarklist.append(item.to_dict())
        print('dict:', item.to_dict())
    print(remarklist)
    return remark_api(remark=remarklist, msg='添加成功')


# 商品详细加评论

@remark.route('/goodsdetatilremark', methods=["POST", "GET"])
def goodsdetatilremark():
    req_json = request.json
    gid = req_json.get('id')
    good = Goods.query.filter(Goods.id == gid).first()
    remarks = Goods_Remark.query.filter(Goods_Remark.gid == gid).all()
    remarklist = []
    for item in remarks:
        user=WxUser.query.filter(WxUser.openid==item.openid).first()
        remark=item.to_dict()
        remark['user']=user.to_dict()
        remarklist.append(remark)
    goodlist=[]
    goodlist.append(good.to_dict())
    print(remarklist)

    return remark_api(remark=remarklist,good=goodlist)


# 用户查看评论
@remark.route('/myremarklist', methods=["POST", "GET"])
def myremarklist():
    req_json=request.json
    openid=xss_escape(req_json.get('openid'))
    good_remark=Goods_Remark.query.filter(Goods_Remark.openid==openid).all()
    remarklist=[]
    for remark in good_remark:
        rem={}
        good=Goods.query.get(remark.gid)
        rem['good']=good.to_dict()
        rem['remark']=remark.to_dict()
        remarklist.append(rem)
    print(remarklist)
    return remark_api(errno=0,remark=remarklist)

# 删除用户评论
@remark.route('/delremark', methods=["POST", "GET"])
def delremark():
    req_json = request.json
    id = req_json.get('id')
    res=Goods_Remark.query.filter(Goods_Remark.id==id).delete()
    db.session.commit()

    print(res)
    return remark_api(errno=0)